package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.autoconfig.templates.AipFaceTemplate;
import com.tanhua.autoconfig.templates.OssTemplate;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.User;
import com.tanhua.model.db.UserInfo;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class UsersService {
    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private OssTemplate template;

    @Autowired
    private AipFaceTemplate aipFaceTemplate;

    //根据userId查询用户信息
    public UserInfoVo findUserInfoById(Long userId) {
        //1. 查询数据
        UserInfo userInfo = userInfoApi.findById(userId);
        //2. 将对象转换为vo
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, vo);
        //不相同的，需要手动设置
        if (userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        //3. 返回vo对象
        return vo;
    }

    //更新用户信息
    public void updateUserInfo(UserInfo userInfo) {
        userInfoApi.update(userInfo);
    }

    //查询通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        Long userId = UserHolder.getUserId();

        //1. 调用api，查询当前用户的陌生人问题（根据用户id查询）
        Question question = questionApi.findByUserId(userId);
        String txt = question == null ? "你喜欢java吗？" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //2. 调用api，查询用户通知开关
        Settings settings = settingsApi.findById(userId);
        if (settings != null) {
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
            vo.setGonggaoNotification(settings.getGonggaoNotification());
        }
        //3. 获取当前用户的手机号
        User user = UserHolder.getUser();
        String mobile = user.getMobile();
        vo.setPhone(mobile);
        vo.setId(userId);
        //4. 返回

        return vo;
    }

    //设置陌生人问题保存或更新
    public void questions(String content) {
        //1.获取对象id
        Long userId = UserHolder.getUserId();

        //2.查询对象问题
        Question question = questionApi.findByUserId(userId);

        //3.无问题，添加
        if (question == null) {
            question = new Question();
            question.setTxt(content);
            question.setUserId(userId);
            questionApi.save(question);
        } else {
            //4.有问题，更新
            question.setTxt(content);
            questionApi.update(question);
        }

    }

    //保存通知设置
    public void setting(Boolean like, Boolean pinglun, Boolean gonggao) {
        //1. 获取用户id
        Long userId = UserHolder.getUserId();

        //2. 查询通知开关对象
        Settings settings = settingsApi.findById(userId);

        //3. 如果对象不存在，保存
        if (settings == null) {
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(like);
            settings.setPinglunNotification(pinglun);
            settings.setGonggaoNotification(gonggao);
            settingsApi.save(settings);
        } else {
            //4. 如果对象存在，更新
            settings.setLikeNotification(like);
            settings.setPinglunNotification(pinglun);
            settings.setGonggaoNotification(gonggao);
            settingsApi.update(settings);
        }
    }

    //黑名单分页查询
    public PageResult blacklist(Integer page, Integer pageSize) {
        //1. 或取用户id
        Long userId = UserHolder.getUserId();

        //2. 调用api查询，黑名单信息
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId, page, pageSize);

        //3. 将Ipage转换成PageResult对象
        return new PageResult(page, pageSize, iPage.getTotal(), iPage.getRecords());
    }

    //黑名单用户删除
    public void delBlacklist(Long blackUserId) {
        //1. 获取用户id
        Long userId = UserHolder.getUserId();

        //2. 调用api，删除黑名单信息
        blackListApi.delete(userId, blackUserId);

    }

    //更新用户头像
    public void head(MultipartFile headPhoto, Long id) throws IOException {
        //1. 上传到阿里云oss
        String url = template.upload(headPhoto.getInputStream(), headPhoto.getOriginalFilename());

        //2. 判断头像中是否包含人脸
        Boolean detect = aipFaceTemplate.detect(url);
        if (!detect) {
            // TODO by 不包含人脸,在阿里云中进行删除-------------------------------作业
            template.deleteImage(url);
            throw new BusinessException(ErrorResult.faceError());
        }

        //3. 调用api，更新userinfo
        UserInfo userInfo = new UserInfo();
        userInfo.setId(id);
        userInfo.setAvatar(url);
        userInfoApi.update(userInfo);

    }
}
